package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.domain.Role;
import com.itheima.mapper.RoleMapper;
import com.itheima.service.IRoleService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 后台用户角色表 服务实现类
 * </p>
 *
 * @author fukeke
 * @since 2022-11-11
 */
@DubboService
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public Role findById(Long roleId) {
        return roleMapper.selectById(roleId);
    }

    @Override
    public List<Role> findAll() {
        LambdaQueryWrapper<Role> qw=new LambdaQueryWrapper<>();
        qw.eq(Role::getStatus,1);
        return roleMapper.selectList(qw);
    }

    @Override
    public PageBeanVo findByConditionAndPage(Integer pageNum, Integer pageSize, String keyword) {
        LambdaQueryWrapper<Role> qw=new LambdaQueryWrapper<>();
        //匹配关键字
        if(keyword!=null){
            qw.like(Role::getName,keyword);
        }
        //添加分页条件
        Page<Role> page=new Page<>(pageNum,pageSize);

        roleMapper.selectPage(page,qw);

        //封装对象
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());

    }

    @Override
    public void saveRole(Role role) {
        roleMapper.insert(role);
    }

    @Override
    public void deleteByIds(List<Long> roleIds) {
        roleMapper.deleteBatchIds(roleIds);
    }

    @Override
    public void updateRole(Role role) {
        roleMapper.updateById(role);
    }
}
